package pers.qianyu.month_202102.date_20210211;

import java.util.*;

/**
 * 703. 数据流中的第 K 大元素
 * https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/
 *
 * @author mizzle rain
 * @date 2021年2月11日23:42:45
 */
public class KthLargest {
    private PriorityQueue<Integer> q = new PriorityQueue<>();
    private int k;

    public KthLargest(int k, int[] nums) {
        this.k = k;
        for (int x : nums) {
            q.offer(x);
            if (q.size() > k) {
                q.poll();
            }
        }
    }

    public int add(int val) {
        q.offer(val);
        if (q.size() <= k) {
            return q.peek();
        }
        q.poll();
        return q.peek();
    }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.add(val);
 */